package com.rd.project.controller;

import com.itextpdf.text.DocumentException;
import com.rd.project.model.Klas;
import com.rd.project.model.Score;
import com.rd.project.model.Student;
import com.rd.project.model.Test;
import com.rd.project.model.Vak;
import com.rd.project.service.DataService;
import java.io.IOException;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Stefan
 */
@ManagedBean(name="dataController")
@ViewScoped
public class DataController {
    
    private int selectedKlas = 0;
    private int selectedVak = 0;
    private int selectedTest = 0;
    private Test test = null;
    
    @EJB
    private DataService dataService;
    
    public List<Klas> getAlleKlassen() {
        return dataService.getAlleKlassen();
    }
    
    public List<Vak> getAlleVakkenFromKlas() {
        return dataService.getAlleVakkenFromKlas(this.selectedKlas);
    }
    
    public List<Test> getAlleTesten() {
        return dataService.getAlleTestenFromVak(this.selectedVak);
    }
    
    public List<Score> getAlleScores() {
        System.out.println("TestId:" + this.selectedTest);
        if(this.selectedTest != 0) {
           test = dataService.getTest(this.selectedTest);
        }
        return dataService.getAlleScoresFromTest(this.selectedTest);
    }

    public int getSelectedKlas() {
        return selectedKlas;
    }

    public void setSelectedKlas(int selectedKlas) {
        this.selectedKlas = selectedKlas;
    }

    public int getSelectedVak() {
        return selectedVak;
    }

    public void setSelectedVak(int selectedVak) {
        this.selectedVak = selectedVak;
    }

    public int getSelectedTest() {
        return selectedTest;
    }

    public void setSelectedTest(int selectedTest) {
        this.selectedTest = selectedTest;
    }

    public Test getTest() {
        return test;
    }

    public void setTest(Test test) {
        this.test = test;
    }
    
    public Student getStudent(int studentId)  {
        return dataService.getStudent(studentId);
    }
    
    public double getGemiddelde() {
        double gemiddelde = 0;
        System.out.println("Hello World");
        for(int i = 0; i < getAlleScores().size(); i++) {
            gemiddelde += getAlleScores().get(i).getScore();
        }
        if(gemiddelde != 0) {           
            return ((gemiddelde / getAlleScores().size()) / test.getTotaal()) * 100;
        } else {
            return 0;
        }      
    }
    
    public void genereerPDFTest() 
            throws DocumentException, IOException {
        dataService.genereerPdfTest(selectedTest);
    }
    
    public void genereerPDFVak() 
            throws DocumentException, IOException {
        dataService.genereerPdfVak(selectedVak);
    }
    
    public void genereerPDFKlas() 
            throws DocumentException, IOException {
        dataService.genereerPdfKlas(selectedKlas);
    }
}
